package leetcode.part2;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class S68 {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> res=new ArrayList<>();
        int i = 0;
        while (i< words.length){
            int[] detect = detect(words, i, maxWidth);
            String s = fillSpace(words, i, detect[0], detect[1], maxWidth);
            res.add(s);
            i=detect[0]+1;
        }
        return res;
    }

    /**
     * 给定一个起始位置（包括这个位置，算出）
     *
     * @param words      数组
     * @param startIndex 起始位置
     * @return int[0]最终位置 包括这个位置，int[1]总长度不包括空格
     *
     */
    private int[] detect(String[] words, int startIndex, int maxLen) {
        int[] res=new int[2];
        int len = 0;
        int i = 0;
        int sumLen=0;
        for (i = startIndex; i < words.length; i++) {
            // 加1插入空格
            int tmpLen = len + words[i].length() + 1;
            sumLen+=words[i].length();
            // 由于总是会往最后字符添加空格，因此判断时需要剪除掉那个空格
            if (tmpLen-1==maxLen){
                len=tmpLen-1;
                break;
            }
            if (tmpLen > maxLen) {
                break;
            }
            len = tmpLen;
        }
        res[0]=i-1;
        // 如果已经达到末尾，就直接返回这个长度，不用减后一位的
        if (i< words.length && len<maxLen ){
            res[1]=sumLen-words[i].length();
        }else{
            res[1]=sumLen;
        }


        return res;
    }

    /**
     * 输入一系列字符串吗，返回构建好的符合要求的字符串
     * @param words
     * @param startIndex
     * @param endIndex
     * @return
     */
    private String fillSpace(String[] words,int startIndex,int endIndex,int sumLen,int maxLen){
        int spaces=maxLen-sumLen;
        int part=endIndex-startIndex;
        if (part<=1) part=1;
        StringBuilder[] partArray=new StringBuilder[part];
        for (int i = 0; i < partArray.length; i++) {
            partArray[i]=new StringBuilder();
        }
        int index=0;
        while (spaces>0){
            partArray[index++].append(" ");
            if (index==partArray.length) {
                index=0;
            }
            spaces--;
        }
        StringBuilder res=new StringBuilder();
        int i=startIndex;
        int j=0;
        while (i<=endIndex&&j<partArray.length){
            res.append(words[i]);
            res.append(partArray[j]);
            i++;
            j++;
        }
        if (i==endIndex){
            res.append(words[i]);
        }
        if (j==partArray.length-1){
            res.append(partArray[j]);
        }
        return res.toString();

    }

    @Test
    public void test() {
        String[] words = {"This", "is", "an", "example", "of", "text", "justification."};
        int maxWidth = 16;
//        System.out.println(Arrays.toString(detect(words, 0, maxWidth)));
//        System.out.println(Arrays.toString(detect(words,3,maxWidth)));
//        System.out.println(Arrays.toString(detect(words,6,maxWidth)));
//        System.out.println(fillSpace(words,0,2,8,16));
//        System.out.println(fillSpace(words,3,5,13,16));
//        System.out.println(fillSpace(words,6,6,14,16));

//        System.out.println(fullJustify(words,maxWidth));
//        String[] words1={"What","must","be","acknowledgment","shall","be"};
        String[] words2={"Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"};
//        System.out.println(fullJustify(words1,maxWidth));
        System.out.println(fullJustify(words2,20));

    }
}
